package com.lagou.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lagou.dao.UserMapper;
import com.lagou.domain.*;
import com.lagou.service.UserService;
import com.lagou.utils.Md5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo findAllUserByPage(UserVo userVo) {

        PageHelper.startPage(userVo.getCurrentPage(),userVo.getPageSize());
        List<User> allUserByPage = userMapper.findAllUserByPage(userVo);

        PageInfo<User> pageInfo = new PageInfo<>(allUserByPage);
        return pageInfo;
    }

    /*
    * 用户登录
    * */
    public User login(User user) throws Exception {
        //调用mapper方法 user2中包含了密文密码
        User user2 =userMapper.login(user);

        if(user2 != null && Md5.verify(user.getPassword(),"lagou",user2.getPassword())){
            return user2;
        }else{
            return null;
        }
    }

    @Override
    public List<Role> findUserRelationRoleById(Integer id) {
        List<Role> list = userMapper.findUserRelationRoleById(id);

        return list;
    }

    @Override
    public void userContextRole(UserVo userVo) {
        //1、根据用户ID清空中间表关联关系
        userMapper.deleteUserContextRole(userVo.getUserId());

        //2、再从新建立关系
        for(Integer roleid : userVo.getRoleIdList()){
            //封装数据
            User_Role_relation user_role_relation = new User_Role_relation();
            user_role_relation.setUserId(userVo.getUserId());
            user_role_relation.setRoleId(roleid);

            Date date = new Date();
           user_role_relation.setCreatedTime(date);
           user_role_relation.setUpdatedTime(date);

           user_role_relation.setCreatedBy("system");
           user_role_relation.setUpdatedby("system");

        }
    }

    @Override
    public ResponseResult getUserPermissions(Integer userid) {

        //1、获取当前用户所拥有的角色
        List<Role> roleList = userMapper.findUserRelationRoleById(userid);

        //2、获取角色ID,保存到List集合
        ArrayList<Integer> roleIds = new ArrayList<>();

        for(Role role:  roleList){
            roleIds.add(role.getId());
        }

        //3、根据角色ID查询父菜单
        List<Menu> parentMenuByRoleId = userMapper.findParentMenuByRoleId(roleIds);

        //4、查询封装父菜单关联的子菜单
        for(Menu menu:parentMenuByRoleId){
            List<Menu> parentMenuByRoleId1 = userMapper.findSubMenuByPid(menu.getId());

            menu.setSubMenuList(parentMenuByRoleId1);
        }
        //5、获取资源信息
        List<Resource> resourceByRoleId = userMapper.findResourceByRoleId(roleIds);

        //6、封装数据并返回
        Map<String,Object> map = new HashMap<>();
        map.put("menuList",parentMenuByRoleId);
        map.put("resourceList",resourceByRoleId);

        return new ResponseResult(true,200,"获取用户权限信息成功",map);
    }
}
